//! RSA操作
use rsa::RsaPublicKey;
use rsa::{
    pkcs8::{DecodePrivateKey, DecodePublicKey},
    RsaPrivateKey,
};

use crate::error::{Error, ErrorKind};

/// key: Pkcs8 pem字符串 -----BEGIN PUBLIC KEY-----
pub fn get_public_key(key: &str) -> Result<RsaPublicKey, Error> {
    RsaPublicKey::from_public_key_pem(key)
        .map_err(|e| Error::from_kind(ErrorKind::Msg(e.to_string())))
}

/// key: pkcs8 pem字符串 -----BEGIN PRIVATE KEY-----
pub fn get_private_key(key: &str) -> Result<RsaPrivateKey, Error> {
    RsaPrivateKey::from_pkcs8_pem(key).map_err(|e| Error::from_kind(ErrorKind::Msg(e.to_string())))
}
